package com.cn.unarmed.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.unarmed.activity.R;
import com.cn.unarmed.bean.DataHolder;

import java.util.List;

/**
 * 包含子列表的listView.
 */
public class ListChildAdapter extends BaseExpandableListAdapter {

    Context context;
    List<DataHolder> list;
    ExpandableListView listView;

    public ListChildAdapter(Context context, List<DataHolder> list,ExpandableListView listView) {
        this.context = context;
        this.list = list;
        this.listView = listView;
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).list_child.get(childPosition);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).list_child.size();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentHolder parentHolder;
        if (convertView == null) {
            if (list.get(groupPosition).list_child.size() == 0){
                convertView = LayoutInflater.from(context).inflate(R.layout.item_list_none, null);
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_list, null);
            }
            parentHolder = new ParentHolder();
            parentHolder.image_parent = (ImageView) convertView.findViewById(R.id.iv_item_img);
            parentHolder.image_btn = (ImageView) convertView.findViewById(R.id.iv_item_btn);
            parentHolder.text_parent = (TextView) convertView.findViewById(R.id.tv_time_exe);
//            parentHolder.view_top = convertView.findViewById(R.id.view_top);
//            parentHolder.view_bottom = convertView.findViewById(R.id.view_bottom);
//            if (groupPosition == 0) {
//                parentHolder.view_top.setVisibility(View.INVISIBLE);
//                parentHolder.view_bottom.setVisibility(View.VISIBLE);
//            }
//            if (groupPosition == list.size() - 1) {
//                parentHolder.view_top.setVisibility(View.VISIBLE);
//                parentHolder.view_bottom.setVisibility(View.INVISIBLE);
//            }
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }
        Log.i("vasdadwdeqwe", String.valueOf(groupPosition));
        parentHolder.image_parent.setImageResource(R.mipmap.device_socket);
        parentHolder.text_parent.setText(list.get(groupPosition).titleStr);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_child, null);
            childHolder = new ChildHolder();
            childHolder.text_child = (TextView) convertView.findViewById(R.id.tv_time_exe);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        childHolder.text_child.setText(list.get(groupPosition).list_child.get(childPosition));
        return convertView;
    }

    private class ParentHolder {
        ImageView image_parent;
        TextView text_parent;
//        View view_top;
//        View view_bottom;
        ImageView image_btn;
    }

    private class ChildHolder {
        TextView text_child;
    }
}
